package openGl.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.util.Log;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import customview.ike.com.customviewproject.R;
import openGl.Mallet;
import openGl.Table;

import static android.opengl.GLES20.GL_LINEAR_MIPMAP_LINEAR;

/**
 * Created by Administrator on 2017/12/28.
 */

public class ColorRender implements GLSurfaceView.Renderer {
    private ColorShaderProgram colorShaderProgam;
    private Context mContext;
    private Table table;
    private Mallet mallet;
    private float[] projectMatrix=new float[16];
    private float[] temp=new float[16];
    private TexureShaderProgram texureShaderProgram;
    private int texture;

    public ColorRender(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
        //colorShaderProgam = new ColorShaderProgram(mContext);
        texureShaderProgram=new TexureShaderProgram(mContext);
        table=new Table(texureShaderProgram);
        texture = createTexture();
        // mallet = new Mallet(colorShaderProgam);

    }
    private float[] newProjectMatrix=new float[16];
    private float[] modelMatrix=new float[16];
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        float radio=width>height?width*1.0f/height:height*1.0f/width;
        if (width>height){
            Matrix.orthoM(projectMatrix,0,-radio,radio,-radio,radio,-1,1);
        }else {
            Matrix.orthoM(projectMatrix,0,-1,1,-1,1,-1,1);
        }
        //使用透视投影矩阵，设置观察的角度
//        Matrix.perspectiveM(projectMatrix, 0,45, (float) width
//                / (float) height, 1f, 10f);
//        //设置modelMatrix为标准单位矩阵
//        Matrix.setIdentityM(modelMatrix,0);
//        Matrix.translateM(modelMatrix,0,0,0,-2.8f);
//        //绕x轴旋转60度
//        Matrix.rotateM(modelMatrix,0,-60,1,0,0);
//        Matrix.multiplyMM(temp,0,projectMatrix,0,modelMatrix,0);
//        System.arraycopy(temp,0,projectMatrix,0,temp.length);

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        texureShaderProgram.useProgram();
       texureShaderProgram.setUniforms(projectMatrix,texture);
        table.draw();
//        colorShaderProgam.useProgram();
//        //mallet.bindData(colorShaderProgam);
//        mallet.draw();
    }
    /**
     * 加载纹理图片
     */
    private int createTexture() {

        int[] textureId = new int[1];
        //生成纹理对象
        GLES20.glGenTextures(1, textureId, 0);
       // Log.e(Tag, "纹理对象id:" + textureId[0]);
        //加载本地图片生成Bitmap对象.openGl只能加载原始数据而不是压缩数据
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;//设置获取图片的原始数据，而不是压缩数据
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.air_hockey_surface, options);
        if (bitmap == null) {
           // Log.e(Tag, "图片加载失败");
        }
        //绑定纹理对象
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
        //设置纹理图片显示的时候的过滤器
        //设置图片放大的时候图片的处理方式(三线性过滤)
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        //设置图片缩小的时候图片的处理方式(双线性过滤)
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        //加载图片到openGl中
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
        //生成MIP贴图
        GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
        //解除与纹理对象的绑定(0表示解除绑定)
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        return textureId[0];
    }
}
